package org.yxt.springclod;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;

/**
 * 使用hystrix保证系统正常运行
 *
 * @author Yang douya
 * @date 2022-01-03
 */
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker // 启动服务降级
public class PaymentHystrixMain8001 {

    public static void main(String[] args) {
        SpringApplication.run(PaymentHystrixMain8001.class, args);
    }

    /**
     *
     * 此配置是为了服务监控配置，与服务本身无关，spring cloud 升级之后的坑 没有做修改会报 Unable to connect to Command Metric Stream 异常
     * ServletRegistrationBean因为spring boot默认路径不是/hystrix.stream
     * 只要在自己项目上配置一下bean即可修正
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}
